-- Copyright (c) 2015,2016 Lucky Byte, Inc.
--
-- Summary : ACP 实时代付模块
--           检查代付规则限制
--
-- Author  : 吴小虎 <xiaohu@lucky-byte.com>
-- Since   : 2016.4.29
--
-- History
--  * 2015.4.29 吴小虎 : 创建此文件
--

-- 检查交易环境
if not reqt or not p1 then
    logger.error('交易执行环境不完整，缺少 reqt or p1 ...')
    return nil
end


-- 检查当日代付最大金额/笔数
local _check_max_of_day = function(trans_amt)
    local _max_amt = p1.record.get('max_amt_day') or 0.0
    local _max_cnt = p1.record.get('max_cnt_day') or 0

    if _max_amt == 0.0 and _max_cnt == 0 then
        return '00'
    end

    local _table = jdbc.select([[
        select
            cast(count(uuid) as int) as count,
            sum(cast(json_extract_path_text(i_pkt,'txnAmt') as decimal)) / 100 as amt
        from pay_trlist
        where
            status = 0 and incoming = false and chnl = 3 and
            sys_time >= CURRENT_DATE and sys_time <= now() and
            json_extract_path_text(i_pkt, 'txnType') = '12' and
            json_extract_path_text(i_pkt, 'txnSubType') = '00' and
            json_extract_path_text(i_pkt, 'bizType') = '000401' and
            json_extract_path_text(i_pkt, 'merId') = ? and
            json_extract_path_text(o_pkt, 'respCode') = '00'
    ]], p1.merch.get('merno'))
    assert(_table)
    local _record = _table.get_record(0)

    -- 需要检查金额
    if _max_amt > 0.0 then
        local _amt = _record.get('amt') or 0.0
        if _amt > 0.0 and _amt + trans_amt  >= _max_amt then
            logger.error(
                '当日累计代付金额[%s]+当前代付金额[%s]已达到或超出单日代付限额[%s]，停止代付.',
                tostring(_amt), tostring(p1.amt), tostring(_max_amt))
            return '06'
        end
    end

    -- 需要检查笔数
    if _max_cnt > 0 then
        local _count = _record.get('count')
        if _count >= _max_cnt then
            logger.error(
                '当日累计代付笔数[%s]已达到或超出单日代付笔数限额[%s]，停止代付.',
                tostring(_count), tostring(_max_cnt))
            return '07'
        end
    end
    return '00'
end


-- 检查本周代付最大金额/笔数
--
local _check_max_of_week = function(trans_amt)
    local _max_amt = p1.record.get('max_amt_week') or 0.0
    local _max_cnt = p1.record.get('max_cnt_week') or 0

    if _max_amt == 0.0 and _max_cnt == 0 then
        return '00'
    end

    local _table = jdbc.select([[
        select
            cast(count(uuid) as int) as count,
            sum(cast(json_extract_path_text(i_pkt,'txnAmt') as decimal)) / 100 as amt
        from pay_trlist
        where
            status = 0 and incoming = false and chnl = 3 and
            extract(week from sys_time) = ?::int and
            json_extract_path_text(i_pkt, 'txnType') = '12' and
            json_extract_path_text(i_pkt, 'txnSubType') = '00' and
            json_extract_path_text(i_pkt, 'bizType') = '000401' and
            json_extract_path_text(i_pkt, 'merId') = ? and
            json_extract_path_text(o_pkt, 'respCode') = '00'
    ]], core.date():fmt('%W'), p1.merch.get('merno'))
    assert(_table)
    local _record = _table.get_record(0)

    -- 需要检查金额
    if _max_amt > 0.0 then
        local _amt = _record.get('amt') or 0.0
        if _amt > 0.0 and _amt + trans_amt  >= _max_amt then
            logger.error(
                '本周累计代付金额[%s]+当前代付金额[%s]已达到或超出本周代付金额限额[%s]，停止代付.',
                tostring(_amt), tostring(p1.amt), tostring(_max_amt))
            return '06'
        end
    end
    -- 需要检查笔数
    if _max_cnt > 0 then
        local _count = _record.get('count')
        if _count >= _max_cnt then
            logger.error(
                '本周累计代付笔数[%s]已达到或超出本周代付笔数限额[%s]，停止代付.',
                tostring(_count), tostring(_max_cnt))
            return '07'
        end
    end
    return '00'
end


-- 检查本月代付最大金额/笔数
local _check_max_of_month = function(trans_amt)
    local _max_amt = p1.record.get('max_amt_month') or 0.0
    local _max_cnt = p1.record.get('max_cnt_month') or 0

    if _max_amt == 0.0 or _max_cnt == 0 then
        return '00'
    end

    local _table = jdbc.select([[
        select
            cast(count(uuid) as int) as count,
            sum(cast(json_extract_path_text(i_pkt,'txnAmt') as decimal)) / 100 as amt
        from pay_trlist
        where
            status = 0 and incoming = false and chnl = 3 and
            extract(year from sys_time) = ?::int and
            extract(month from sys_time) = ?::int and
            json_extract_path_text(i_pkt, 'txnType') = '12' and
            json_extract_path_text(i_pkt, 'txnSubType') = '00' and
            json_extract_path_text(i_pkt, 'bizType') = '000401' and
            json_extract_path_text(i_pkt, 'merId') = ? and
            json_extract_path_text(o_pkt, 'respCode') = '00'
    ]], os.date('%Y'), os.date('%m'), p1.merch.get('merno'))
    assert(_table)
    local _record = _table.get_record(0)

    -- 需要检查金额
    if _max_amt > 0.0 then
        local _amt = _record.get('amt') or 0.0
        if _amt > 0.0 and _amt + trans_amt  >= _max_amt then
            logger.error(
                '本月累计代付金额[%s]+当前代付金额[%s]已达到或超出本月代付金额限额[%s]，停止代付.',
                tostring(_amt), tostring(p1.amt), tostring(_max_amt))
            return '06'
        end
    end
    -- 需要检查笔数
    if _max_cnt > 0 then
        local _count = _record.get('count')
        if _count >= _max_cnt then
            logger.error('本月累计代付笔数[%s]已达到或超出本月代付笔数限额[%s]，停止代付.',
                tostring(_count), tostring(_max_cnt))
            return '07'
        end
    end
    return '00'
end


-- 检查代付规则限制
return function()
    local _rescode = _check_max_of_day(p1.amt)
    if _rescode ~= '00' then
        return _rescode
    end
    local _rescode = _check_max_of_week(p1.amt)
    if _rescode ~= '00' then
        return _rescode
    end
    local _rescode = _check_max_of_month(p1.amt)
    if _rescode ~= '00' then
        return _rescode
    end
    return '00'
end
